using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using Screen = TVDesigner.Entities.Elements.Screen;
using TVDesigner.Entities.Elements;

namespace TVDesigner.GUI.Controls
{
    public partial class DiagramsList : UserControl
    {
        private List<DiagramPanel> _Diagrams;
        private FMain _MainForm;

        public FMain MainForm
        {
            get { return _MainForm; }
            set { _MainForm = value; }
        }
	

        public DiagramsList()
        {
            InitializeComponent();
            _Diagrams = new List<DiagramPanel>();
            _ComboTelaInicial.DisplayMember = "Name";
            
        }

        #region Private Methods

        private void btnAdd_Click(object sender, EventArgs e)
        {
            _AddNewDiagram(new DiagramPanel(), true);
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (_ListViewScreens.SelectedItems.Count > 0)
            {
                ListViewItem item = _ListViewScreens.SelectedItems[0];
                _RemoveDiagram(item);
            }
        }

        private void _RemoveDiagram(ListViewItem item)
        {
            DiagramPanel diagram = item.Tag as DiagramPanel;
            _ListViewScreens.Items.Remove(item);
            _MainForm.Project.RemoveScreen(diagram.Screen);
            _Diagrams.Remove(diagram);
            _MainForm.DiagramTabControl.CloseDiagram(diagram);
            _ComboTelaInicial.Items.Remove(diagram);
        }

        private void _AddNewDiagram(DiagramPanel diagram, bool openDiagram)
        {
            _MainForm.Project.AddScreen(diagram.Screen);
            diagram.Screen.SetProject(_MainForm.Project);

            _AddDiagram(diagram, openDiagram);
        }

        private void _AddDiagram(DiagramPanel diagram, bool openDiagram)
        {
            ListViewItem item = new ListViewItem(diagram.Name);
            item.Tag = diagram;
            _ListViewScreens.Items.Add(item);
            if (openDiagram)
            {
                _MainForm.DiagramTabControl.OpenDiagram(diagram);
            }           
            
            _Diagrams.Add(diagram);     
            _ComboTelaInicial.Items.Add(diagram);
        }

        private void _ListViewScreens_DoubleClick(object sender, EventArgs e)
        {
            if (_ListViewScreens.SelectedItems.Count > 0)
            {
                ListViewItem item = _ListViewScreens.SelectedItems[0];
                DiagramPanel diagram = item.Tag as DiagramPanel;
                _MainForm.ShowDiagramTabControl(diagram);
            }
        }

        private void _ComboTelaInicial_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_ComboTelaInicial.SelectedItem != null)
            {
                _MainForm.Project.StartScreen = ((DiagramPanel)(_ComboTelaInicial.SelectedItem)).Name;
                _MainForm.ScreensPanel.Invalidate();
            }
            else
            {
                _MainForm.Project.StartScreen = null;
            }
        }

        #endregion

        public void NewProject()
        {
            _ListViewScreens.Items.Clear();
        }

        public void OpenProject()
        {
            _ListViewScreens.Items.Clear();            

            List<TVDesigner.Entities.Elements.Screen> screens = _MainForm.Project.Screens;

            foreach (TVDesigner.Entities.Elements.Screen screen in screens)
            {
                DiagramPanel diagram = new DiagramPanel(_MainForm, screen);
                _AddDiagram(diagram, false);

                if (_MainForm.Project.StartScreen != null && _MainForm.Project.StartScreen.Equals(screen.Name))
                {
                    _ComboTelaInicial.SelectedItem = diagram;
                }
            }            
        }

        public void CloseProject()
        {
            while (_ListViewScreens.Items.Count > 0)
            {
                _RemoveDiagram(_ListViewScreens.Items[0]);
            }
        }

        public void RemoveDiagram(TVDesigner.Entities.Elements.Screen screen)
        {
            foreach (ListViewItem item in _ListViewScreens.Items)
            {
                DiagramPanel panel = item.Tag as DiagramPanel;
                if (panel.Screen.Equals(screen))
                {
                    _RemoveDiagram(item);
                    break;
                }
            }
        }

        private void btnRename_Click(object sender, EventArgs e)
        {
            if (_ListViewScreens.SelectedItems.Count > 0)
            {
                string nome = _ListViewScreens.SelectedItems[0].Text;

                using (FRename fRename = new FRename(_MainForm, nome))
                {
                    if (fRename.ShowDialog() == DialogResult.OK)
                    {
                        string novoNome = fRename.NovoNome;
                        _ListViewScreens.SelectedItems[0].Text = novoNome;
                        DiagramPanel diagram = _ListViewScreens.SelectedItems[0].Tag as DiagramPanel;
                        diagram.Name = novoNome;
                        diagram.Parent.Text = novoNome;
                        diagram.Screen.Name = novoNome;

                        DiagramPanel selectedDiagram = _ComboTelaInicial.SelectedItem as DiagramPanel;

                        _ComboTelaInicial.Items.Clear();
                        foreach (DiagramPanel d in _Diagrams)
                        {
                            _ComboTelaInicial.Items.Add(d);
                        }

                        if (_MainForm.Project.StartScreen != null
                            && _MainForm.Project.StartScreen.Equals(nome))
                        {

                            _MainForm.Project.StartScreen = novoNome;
                        }

                        _ComboTelaInicial.SelectedItem = selectedDiagram;
                    }
                }
            }
        }
    }
}

